package org.springblade.modules.admin.controller;

import cn.hutool.core.collection.ListUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.admin.dto.plan.BatchModifyPlanUserDTO;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.vo.*;
import org.springblade.modules.admin.vo.task.PlanCheckParam;
import org.springblade.modules.admin.vo.task.PlanTipVO;
import org.springblade.modules.admin.vo.task.SpeedPlanParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.admin.entity.MaintainPlanConfig;
import org.springblade.modules.admin.wrapper.MaintainPlanConfigWrapper;
import org.springblade.modules.admin.service.IMaintainPlanConfigService;
import org.springblade.core.boot.ctrl.BladeController;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 保养计划配置 控制器
 *
 * @author ysq
 * @since 2022-03-09
 */
@RestController
@AllArgsConstructor
@RequestMapping("sida-maintain-admin/maintainplanconfig")
@Api(value = "保养计划配置", tags = "保养计划配置接口")
public class MaintainPlanConfigController extends BladeController {

	private final IMaintainPlanConfigService maintainPlanConfigService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入maintainPlanConfig")
	public R<MaintainPlanConfigVO> detail(MaintainPlanConfig maintainPlanConfig) {
		MaintainPlanConfig detail = maintainPlanConfigService.getOne(Condition.getQueryWrapper(maintainPlanConfig));
		return R.data(MaintainPlanConfigWrapper.build().entityVO(detail));
	}

	/**
	 * 自定义分页 保养计划配置
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入maintainPlanConfig")
	public R<IPage<MaintainPlanConfigVO>> page(MaintainPlanConfigVO maintainPlanConfig, Query query) {
		IPage<MaintainPlanConfigVO> pages = maintainPlanConfigService.selectMaintainPlanConfigPage(Condition.getPage(query), maintainPlanConfig);
		return R.data(pages);
	}

	/**
	 * 修改 保养计划配置
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "修改", notes = "传入maintainPlanConfig")
	public R update(@Valid @RequestBody MaintainPlanConfig maintainPlanConfig) {
		return R.status(maintainPlanConfigService.updateById(maintainPlanConfig));
	}

	@GetMapping("/getBuilidingList")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "获取项目集合")
	public R<List<Map<Long, String>>> getBuildingList(PlanConfigSearchVO search) {
		return R.data(maintainPlanConfigService.getBuilidingList(search));
	}

	@GetMapping("/building/list")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "获取项目集合")
	public R<List<Map<String, Object>>> getBuildingAllList(PlanConfigSearchVO search) {
		return R.data(maintainPlanConfigService.getBuildingAllList(search));
	}

	@GetMapping("/getElevatorList")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "获取梯号集合")
	public R<List<Map<Long, String>>> getElevatorList(PlanConfigSearchVO search) {
		return R.data(maintainPlanConfigService.getElevatorList(search));
	}

	@GetMapping("/getModeTypeList")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "获取梯型式集合")
	public R<List<Map<Long, String>>> getModeTypeList(PlanConfigSearchVO search) {
		return R.data(maintainPlanConfigService.getModeTypeList(search));
	}

	@GetMapping("/getTaskPlanList")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "获取保养计划集合")
	public R<List<TaskPlanListVO>> getTaskPlanList(String keyword) {
		return R.data(maintainPlanConfigService.getTaskPlanList(keyword));
	}

	@PostMapping("/speed/create/plan")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "一键排期")
	public R speedCreatePlan(@RequestBody @Valid SpeedPlanParam speedPlanParam) {
		// 大批量场景
		List<List<Long>> lists = ListUtil.split(Func.toLongList(speedPlanParam.getElevatorIds()), 50);
		for (List<Long> list : lists) {
			SpeedPlanParam itemSpeedPlanParam = new SpeedPlanParam();
			BeanUtils.copyProperties(speedPlanParam, itemSpeedPlanParam, "elevatorIds");
			String elevatorIdsStr = list.stream().map(item -> String.valueOf(item)).collect(Collectors.joining( ","));
			itemSpeedPlanParam.setElevatorIds(elevatorIdsStr);
			maintainPlanConfigService.speedCreatePlan(itemSpeedPlanParam);
		}
		return R.data(true);
	}

	@GetMapping("/getTemplateByElevatorId")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "根据电梯id获取保养模板")
	public R<List<Map<Long,String>>> getTemplateByElevatorId(Long elevatorId) {
		return R.data(maintainPlanConfigService.getTemplateByElevatorId(elevatorId));
	}

	@GetMapping("/planConfigStatistics")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "保养排期统计")
	public R<PlanConfigStatisticsVO> planConfigStatistics(int intervalMonth) {
		return R.data(maintainPlanConfigService.planConfigStatistics(intervalMonth));
	}

	@GetMapping("/planConfigStatisticsPlus")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "保养排期统计")
	public R<PlanCreateStatisticsVO> planConfigStatisticsPlus(@RequestParam int maintainStatus) {
		return R.data(maintainPlanConfigService.planConfigStatisticsPlus(maintainStatus));
	}

	@PostMapping("/editPlanConfig")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "编辑保养配置")
	public R editPlanConfig(@RequestBody SpeedPlanParam speedPlanParam) {
		return R.status(maintainPlanConfigService.editPlanConfig(speedPlanParam));
	}

	@PostMapping("/planCheck")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "一键排期提示判断")
	public R<List<PlanTipVO>> planCheck(@RequestBody @Valid PlanCheckParam planCheckParam) {
		return R.data(maintainPlanConfigService.planCheck(planCheckParam));
	}

	@ApiOperation("列表导出")
	@PostMapping("/down")
	public void down(HttpServletResponse response,@RequestBody MaintainPlanConfigVO maintainPlanConfig) throws IOException {
		maintainPlanConfigService.down(response, maintainPlanConfig);
	}

	/**
	 * @Author lsay
	 * @Description {
	 *    校验保养排期批量更新人员
	 * }
	 * @Date 9:23 2025/6/17
	 * @Param
	 * @return
	 **/
	@PostMapping("/verify/modify/plan/user")
	public R<Map<String,List<VerifyModifyPlanUserResultVO>>> verifyModifyPlanUser(@RequestBody BatchModifyPlanUserDTO userDTO){
		return R.data(maintainPlanConfigService.verifyModifyPlanUser(userDTO));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   保养排期批量更新人员
	 * }
	 * @Date 9:29 2025/6/17
	 * @Param
	 * @return
	 **/
	@PostMapping("/modify/plan/user")
	public R<Boolean> modifyPlanUser(@RequestBody List<VerifyModifyPlanUserResultVO> list){
		return R.data(maintainPlanConfigService.modifyPlanUser(list));
	}

}
